<?php
require_once('model/Garage.php');

class Garage extends DataObjects_Garage
{
	
	/**
	 * Get all category
	 *
	 * @return Category object
	 */
	public function getList($filter = array(), $pagination = true){
		
		$garage = new Garage();
		$garage->joinAdd(new Category());
		$garage->joinAdd(new Package());
		$garage->joinAdd(new Advertisement());
		$garage->selectAdd();
		$garage->selectAdd('garage.*, category.name category_name, package.name package_name, advertisement.name as ad_name');
		if(isset($filter['garage_name']) && $filter['garage_name']){
			$garage->whereAdd('title LIKE "%'.$filter['garage_name'].'%"');
		}
		if(isset($filter['customer_id']) && $filter['customer_id']){
			$garage->customer_id = $filter['customer_id'];
		}
		if(isset($filter['category_id']) && $filter['category_id']){
			$garage->category_id = $filter['category_id'];
		}
		if(isset($filter['package_id']) && $filter['package_id']){
			$garage->package_id = $filter['package_id'];
		}
		
		if($pagination){
			$p = new FRM_Pager('page',Parameter::$params["PAGING_DEFAULT_ITEM_PER_PAGE"], new FRM_Pager_RangeIterator(1, Parameter::$params['PAGING_DEFAULT_PAGE_DELTA'], 1));
			$garage->setPager($p);
		}
		
		$garage->orderBy('id DESC');
		$garage->find();
		return $garage;
	}
	
	public function getGarageNearBy($filter = array(), $pagination = true){
		$garage = new Garage();
		$garage->joinAdd(new Category());
		$garage->selectAdd();
		//seach by m2, replace 6371 to 3959
		$garage->selectAdd("garage.*, category.name category_name, ( 6371 * acos( cos( radians('".$filter['lat']."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$filter['lng']."') ) + sin( radians('".$filter['lat']."') ) * sin( radians( latitude ) ) ) ) AS distance");
		
		if(isset($filter['category_id']) && $filter['category_id']){
			$garage->category_id = $filter['category_id'];
		}
		
		$garage->having("distance < ".$filter['radius']);
		$garage->orderBy('distance');

		if($pagination){
			$p = new FRM_Pager('page',Parameter::$params["PAGING_DEFAULT_ITEM_PER_PAGE"], new FRM_Pager_RangeIterator(1, Parameter::$params['PAGING_DEFAULT_PAGE_DELTA'], 1));
			$garage->setPager($p);
		}
		
		$garage->find();
		return $garage;
	}
	
	public function getListSearch($filter = array()){
		$garage = new Garage();
		$garage->joinAdd(new Category());
		$garage->joinAdd(new Customer());
		$garage->joinAdd(new Package());
		$garage->selectAdd();
		//seach by m2, replace 6371 to 3959
		$garage->selectAdd("garage.*, category.name category_name, DATEDIFF(DATE_FORMAT(NOW(),'%Y-%m-%d'), DATE_FORMAT(customer.date_create, '%Y-%m-%d')) as searchday, package.search_day");
	
		if(isset($filter['category_id']) && $filter['category_id']){
			$garage->category_id = $filter['category_id'];
		}
	
		$garage->having("searchday <= search_day");
		$garage->orderBy('searchday DESC');
		$garage->limit(4);
	
		$garage->find();
	
		return $garage;
	}
	
	public function getGarage($id){
		$garage = new Garage();
		$garage->joinAdd(new Category());
		$garage->joinAdd(new Customer());
		$garage->selectAdd();
		$garage->selectAdd('garage.*, category.name as category_name, customer.telephone, customer.mobilephone, customer.email');
		
		$garage->get($id);
		
		return $garage;
	}
	
	public function getDetailGarage($id){
		$garage = new Garage();
		$garage->joinAdd(new Category());
		$garage->joinAdd(new Package());
		$garage->joinAdd(new Advertisement());
		$garage->selectAdd();
		$garage->selectAdd('garage.*, category.name category_name, package.name package_name, advertisement.name as ad_name');
	
		$garage->get($id);
	
		return $garage;
	}
	
	public function getGarageTop(){
		$garage = new Garage();
		$garage->joinAdd(new Customer());
		$garage->joinAdd(new Package());
		$garage->whereAdd("DATEDIFF(DATE_FORMAT(NOW(),'%Y-%m-%d'), DATE_FORMAT(customer.date_create, '%Y-%m-%d')) <= package.ad_day");
		$garage->orderBy('RAND()');
		$garage->limit(4);
		$garage->find();
		
		return $garage;
	}

}
?>